package com.medvalley.tcm.controller;

import com.medvalley.tcm.base.Result;
import com.medvalley.tcm.pojo.SickPojo;
import com.medvalley.tcm.service.ISickService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(value = "病历相关接口")
public class SickController extends BaseController {

    @Autowired
    private ISickService sickService;

    @ApiOperation(value = "上传病历数据")
    @RequestMapping(value = "/sick/save", method = RequestMethod.POST)
    public Result saveSick(@ModelAttribute @Validated SickPojo sickPojo, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            StringBuffer sb = new StringBuffer();
            List<ObjectError> errors = bindingResult.getAllErrors();
            for (ObjectError err : errors) {
                sb.append(err.getDefaultMessage() + " ");
            }
            return error(sb.toString());
        }
        SickPojo returnSickPojo = sickService.saveSickInfo(sickPojo);
        return success("保存成功", returnSickPojo);
    }

    @ApiOperation(value = "医生诊断")
    @RequestMapping(value = "/sick/update", method = RequestMethod.POST)
    public Result updateSick(@ModelAttribute SickPojo sickPojo) {
        sickService.updateSickInfo(sickPojo);
        return success("保存成功", sickPojo);
    }

    @ApiOperation(value = "根据id获取病历数据")
    @RequestMapping(value = "/sick/get", method = RequestMethod.POST)
    public Result getSick(@RequestParam long id) {
        SickPojo sickPojo = sickService.getSickInfoById(id);
        return success("", sickPojo);
    }

    @ApiOperation(value = "根据barcode获取病历数据")
    @RequestMapping(value = "/sick/get_by_bar_code", method = RequestMethod.POST)
    public Result getSickByBarCode(@RequestParam String barCode) {
        SickPojo sickPojo = sickService.getSickInfoByBarCode(barCode);
        return success("", sickPojo);
    }

    @ApiOperation(value = "根据身份证获取病历数据")
    @RequestMapping(value = "/sick/get_by_id_num", method = RequestMethod.POST)
    public Result getSickByIdNum(@RequestParam String idNum) {
        List<SickPojo> sickPojo = sickService.getSickInfoByIdNum(idNum);
        return success("", sickPojo);
    }

    @ApiOperation(value = "完成诊断，生成报告")
    @RequestMapping(value = "/sick/finish", method = RequestMethod.POST)
    public Result finishReport(@RequestParam long id) {
        sickService.finishReport(id);
        return success(" 完成报告 ");
    }

}
